package br.panthro.media.video
{
	import flash.display.Sprite;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	
	//import org.osmf.net.NetConnectionCodes;
	//import org.osmf.net.NetStreamCodes;


	public class VideoManager extends Sprite
	{
		private var _video:Video = new Video();
		private var _videoPath:String;
		private var _netStream:NetStream;
		private var _netConnection:NetConnection;
		private var _height:uint;
		private var _width:uint;
		private var _autoPlay:Boolean;
		private var _autoRewind:Boolean;
		private var _videoStatus:String;
		private var _duration:Number;
		public var _originalHeight:Number;
		public var _originalWidth:Number;
		
		public function VideoManager()
		{
			super();
		}
		
		public function setVideo(path:String, height:int, width:int, autoPlay:Boolean, autoRewind:Boolean):void
		{
		
			_videoPath = path;
			_height = height;
			_width = width;
			_autoPlay = autoPlay;
			_autoRewind = autoRewind;
			
			init();
		}
		
		private function init():void
		{
			_netConnection = new NetConnection();
			_netConnection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			_netConnection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_netConnection.connect(null);
			_video.height = _height;
			_video.width = _width;
		}
		
		private function netStatusHandler(event:NetStatusEvent):void
		{
			switch (event.info.code) {
				case "NetConnection.Connect.Success":
					connectStream();
					break;
				case "NetStream.Play.StreamNotFound":
					dispatchEvent(new VideoEvent(VideoEvent.LOAD_ERROR));
					break;
				case "NetStream.Play.Stop":
					stop();
					break;
			}
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void
		{
			dispatchEvent(new VideoEvent(VideoEvent.LOAD_ERROR));
		}
		
		private function connectStream():void
		{
			
			_netStream = new NetStream(_netConnection);
			_netStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			_netStream.client = {};
			_netStream.bufferTime = 15;
			_netStream.client.onMetaData = onMetaData;
			
			_video.attachNetStream(_netStream);
			
			_netStream.play(_videoPath);
			trace(_videoPath);
			if(_autoPlay != true)
			{
				_netStream.pause();
			}
			
			addChild(_video);
		}
		
		private function onMetaData(item:Object):void
		{
			_videoStatus = "paused";
			dispatchEvent(new VideoEvent(VideoEvent.LOAD_COMPLETE));
			
			_duration = item.duration;
			_video.smoothing = true;
			
		}
		
		public function play():void
		{
			_netStream.resume();
			_videoStatus = "playing";
			dispatchEvent(new VideoEvent(VideoEvent.VIDEO_PLAYING));
		}
		public function stop():void
		{
			//_netStream.close();
			_videoStatus = "stopped";
			_netStream.seek(0);
			_netStream.pause();
			dispatchEvent(new VideoEvent(VideoEvent.VIDEO_STOPPED));
		}
		public function mute():void
		{
			var transform:SoundTransform = new SoundTransform();
			
			transform.volume = 0;
			
			_netStream.soundTransform = transform;
		}
		public function unmute():void
		{
			var transform:SoundTransform = new SoundTransform();
			
			transform.volume = 1;
			
			_netStream.soundTransform = transform;
		}
		public function pause():void
		{
			_netStream.pause();
			_videoStatus = "paused";
			dispatchEvent(new VideoEvent(VideoEvent.VIDEO_PAUSED));
		}
		public function clear():void
		{
			_netStream.close();
		}
		
		public function get status():String
		{
			return _videoStatus;
		}
		public function set volume(vol:Number):void
		{
			var transform:SoundTransform = new SoundTransform();
			transform.volume = vol/100;
			_netStream.soundTransform = transform;
		}
		public function get volume():Number
		{
			return _netStream.soundTransform.volume;
		}
		public function get currentTime():Number
		{
			return _netStream.time;
		}
		public function get duration():Number
		{
			return _duration;
		}
		public function get percentage():Number
		{
			var percents:Number;
			percents = Math.round((_netStream.time / _duration)*100);
			return percents;
		}
		public function set videoHeight(h:Number):void
		{
			_video.height = h;
		}
		public function set videoWidth(w:Number):void
		{
			_video.width = w;
		}
	}
}